using System;
using System.Diagnostics;
using System.IO;
using System.Collections.Generic;
using org.tmatesoft.svn.core;
using org.tmatesoft.svn.core.wc;

namespace Subsync.Core.Subversion
{
	/// <summary>
	/// Author: Hoang Duc
	/// </summary>
	public class SVNAdminCreate : CommandBase
	{
        /// <summary>
        /// We have our variables declared here.
        /// Access modifiers as you can observe, is public. Why?
        /// Because we trust our programmers.
        /// 
        /// Try to be verbose with the variable names.
        /// Also, try to include *sane defaults*. Take peg_revision for eg.
        /// </summary>   
        private java.io.File path;
        private string uuid;
        private bool enableRevisionProperties; //will be initialized to false at runtime.
        private bool force;
        private bool pre14Compatible;
        private bool pre15Compatible;
        private bool pre16Compatible;

        /// <summary>
        /// Default constructor
        /// </summary>
        public SVNAdminCreate()
        {
        }

        /// <summary>
        /// Minimal constructor
        /// </summary>
        public SVNAdminCreate(String location)
        {
            this.path = new java.io.File(location);
        }

		/// <summary>
		/// Minimal constructor
		/// </summary>
        /// <param name="path">
        /// A <see cref="java.io.File"/>
        /// </param>
        /// <param name="uuid">
        /// A <see cref="System.String"/>
        /// </param>
        /// <param name="enableRevisionProperties">
        /// A <see cref="System.Boolean"/>
        /// </param>
        /// <param name="force">
        /// A <see cref="System.Boolean"/>
        /// </param>
        public SVNAdminCreate(java.io.File path, string uuid, bool enableRevisionProperties, bool force)
        {
            this.path = path;
            this.uuid = uuid;
            this.enableRevisionProperties = enableRevisionProperties;
            this.force = force;
        }

        /// <summary>
        /// Detailed constructor
        /// </summary>
        /// <param name="path">
        /// A <see cref="java.io.File"/>
        /// </param>
        /// <param name="uuid">
        /// A <see cref="System.String"/>
        /// </param>
        /// <param name="enableRevisionProperties">
        /// A <see cref="System.Boolean"/>
        /// </param>
        /// <param name="force">
        /// A <see cref="System.Boolean"/>
        /// </param>
        /// <param name="pre14Compatible">
        /// A <see cref="System.Boolean"/>
        /// </param>
        public SVNAdminCreate(java.io.File path, string uuid, bool enableRevisionProperties, bool force, bool pre14Compatible)
        {
            this.path = path;
            this.uuid = uuid;
            this.enableRevisionProperties = enableRevisionProperties;
            this.force = force;
            this.pre14Compatible = pre14Compatible;
        }

        /// <summary>
        /// Detailed constructor
        /// </summary>
        /// <param name="path">
        /// A <see cref="java.io.File"/>
        /// </param>
        /// <param name="uuid">
        /// A <see cref="System.String"/>
        /// </param>
        /// <param name="enableRevisionProperties">
        /// A <see cref="System.Boolean"/>
        /// </param>
        /// <param name="force">
        /// A <see cref="System.Boolean"/>
        /// </param>
        /// <param name="pre14Compatible">
        /// A <see cref="System.Boolean"/>
        /// </param>
        /// <param name="pre15Compatible">
        /// A <see cref="System.Boolean"/>
        /// </param>
        public SVNAdminCreate(java.io.File path, string uuid, bool enableRevisionProperties, bool force, bool pre14Compatible, bool pre15Compatible)
        {
            this.path = path;
            this.uuid = uuid;
            this.enableRevisionProperties = enableRevisionProperties;
            this.force = force;
            this.pre14Compatible = pre14Compatible;
            this.pre15Compatible = pre15Compatible;
        }

        /// <summary>
        /// Detailed constructor
        /// </summary>
        /// <param name="path">
        /// A <see cref="java.io.File"/>
        /// </param>
        /// <param name="uuid">
        /// A <see cref="System.String"/>
        /// </param>
        /// <param name="enableRevisionProperties">
        /// A <see cref="System.Boolean"/>
        /// </param>
        /// <param name="force">
        /// A <see cref="System.Boolean"/>
        /// </param>
        /// <param name="pre14Compatible">
        /// A <see cref="System.Boolean"/>
        /// </param>
        /// <param name="pre15Compatible">
        /// A <see cref="System.Boolean"/>
        /// </param>
        /// <param name="pre16Compatible">
        /// A <see cref="System.Boolean"/>
        /// </param>
        public SVNAdminCreate(java.io.File path, string uuid, bool enableRevisionProperties, bool force, bool pre14Compatible, bool pre15Compatible, bool pre16Compatible)
        {
            this.path = path;
            this.uuid = uuid;
            this.enableRevisionProperties = enableRevisionProperties;
            this.force = force;
            this.pre14Compatible = pre14Compatible;
            this.pre15Compatible = pre15Compatible;
            this.pre16Compatible = pre16Compatible;
        }

		public override void Execute ()
		{
            //DEBUG MSG. *Always* include this.
            Debug.WriteLine("SVN AdminCreate Execution");

            //library setup
            Tools.SetupLibrary();

            //load up the clients
            SVNClientManager cm = SVNClientManager.newInstance();
            org.tmatesoft.svn.core.wc.admin.SVNAdminClient ac = cm.getAdminClient();

            try
            {
                SVNURL repo = ac.doCreateRepository(this.path, this.uuid, this.enableRevisionProperties, this.force);
                if (repo != null)
                    Debug.WriteLine("Created repository " + repo);

                string[] svnserveConf = File.ReadAllLines(this.path + "/conf/svnserve.conf");
                for (int i = 0; i < svnserveConf.Length; i++)
                {
                    if (svnserveConf[i] == "# anon-access = read")
                        svnserveConf[i] = "anon-access = write";
                }
                File.WriteAllLines(this.path + "/conf/svnserve.conf", svnserveConf);
            }
            catch (SVNException e)
            {
                //we convert this exception to a non SVNKit exception.
                //we want to minimize interfacing with SVNkit outside of this namespace (abstraction)
                throw new SVNExecuteException(e.getMessage());
            }
		}
	}
}
